//
//  ViewController.m
//  TFTPDemo
//
//  Created by 陈冠杰 on 16/7/29.
//  Copyright © 2016年 EzioChen. All rights reserved.
//

#import "ViewController.h"
#import "TftpManager.h"

@interface ViewController (){


    
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//

    
    
    NSString *str = @"这是个测试tftp上传内容的内容。。。突突突！！";
    
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *path = [[TftpManager sharedInstance] getFilePath];
    
    [data writeToFile:path atomically:YES];
    

    
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(transferOver:) name:@"TFTP_TRANSFER" object:nil];
    
    
}



- (IBAction)uploadBtnAction:(id)sender {
    [[TftpManager sharedInstance] initializeWithType:1 fileName:@"tmpTftp.txt" hostName:@"192.168.8.153" Port:69];
}


- (IBAction)downloadBtnAction:(id)sender {
    
    [[TftpManager sharedInstance] initializeWithType:0 fileName:@"UltraISO.lnk" hostName:@"192.168.8.153" Port:69];
}


-(NSString *)getFilePath{

    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *tmpStr = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    tmpStr = [tmpStr stringByAppendingPathComponent:@"tmpTftp"];
    
    if ([fm fileExistsAtPath:tmpStr]) {
        
        return tmpStr;
        
    }else{
        
         [fm createFileAtPath:tmpStr contents:nil attributes:nil];
        
    }
   
    return tmpStr;
}



-(void)transferOver:(NSNotification *)noti{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TFTP" message:@"传输成功" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
